﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LMIS_Application.members
{
    public partial class DelMember : System.Web.UI.Page
    {
        private List<Member> allMembers;
        private int selection;

        protected void Page_Load(object sender, EventArgs e)
        {   
            if (!IsPostBack)
            {
                SetMemberList();
            }
        }

        protected void ddl_DM_Member_SelectedIndexChanged(object sender, EventArgs e)
        {
            selection = Convert.ToInt32(ddl_DM_Member.SelectedValue);
        }

        protected void SetMemberList()
        {
            allMembers = Member.GetMemberList();
            ddl_DM_Member.DataSource = allMembers;
            ddl_DM_Member.DataTextField = "Name";
            ddl_DM_Member.DataValueField = "MemberId";
            ddl_DM_Member.DataBind();
        }

        protected void btnDelMember_Click(object sender, EventArgs e)
        {
            int mid = selection;
            string result = null;
            bool success = false;
            try
            {
                success = Member.DeleteMember(mid);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            finally
            {

                if (success)
                {
                    result = "Member was successfully deleted.";
                }
                else
                {
                    result = "Member could not be deleted or does not exist.";
                }
                msgSuccess.InnerHtml = result;
                SetMemberList();
            }
        }
    }
}
